home *** CD-ROM | disk | FTP | other *** search
/ Aminet 35 / Aminet 35 (2000)(Schatztruhe)[!][Feb 2000].iso / Aminet / game / shoot / ADescentSrc.lha / descent / 2d / bitmap.c < prev    next >
C/C++ Source or Header  |  1998-03-03  |  8KB  |  309 lines

  1. /*
  2. THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
  3. SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
  4. END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
  5. ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
  6. IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
  7. SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
  8. FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
  9. CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
  10. AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.  
  11. COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
  12. */
  13. /*
  14.  * $Source: /usr/CVS/descent/2d/bitmap.c,v $
  15.  * $Revision: 1.1.1.1 $
  16.  * $Author: nobody $
  17.  * $Date: 1998/03/03 15:11:45 $
  18.  *
  19.  * Graphical routines for manipulating grs_bitmaps.
  20.  *
  21.  * $Log: bitmap.c,v $
  22.  * Revision 1.1.1.1  1998/03/03 15:11:45  nobody
  23.  * reimport after crash from backup
  24.  *
  25.  * Revision 1.1.1.1  1998/02/13  20:21:20  hfrieden
  26.  * Initial Import
  27.  *
  28.  * Revision 1.11  1995/08/23  18:46:06  allender
  29.  * fixed compiler warning
  30.  *
  31.  * Revision 1.10  1995/08/14  14:25:45  allender
  32.  * changed transparency color to 0
  33.  *
  34.  * Revision 1.9  1995/07/05  16:04:51  allender
  35.  * transparency/game kitchen changes
  36.  *
  37.  * Revision 1.8  1995/06/15  09:50:48  allender
  38.  * new malloc to align bitmap on 8 byte bountry
  39.  *
  40.  * Revision 1.7  1995/05/12  11:52:19  allender
  41.  * changed memory stuff again
  42.  *
  43.  * Revision 1.6  1995/05/11  12:48:34  allender
  44.  * nge transparency color
  45.  *
  46.  * Revision 1.5  1995/05/04  19:59:21  allender
  47.  * use NewPtr instead of malloc
  48.  *
  49.  * Revision 1.4  1995/04/27  07:33:04  allender
  50.  * rearrange functions
  51.  *
  52.  * Revision 1.3  1995/04/19  14:37:17  allender
  53.  * removed dead asm code
  54.  *
  55.  * Revision 1.2  1995/04/18  12:04:51  allender
  56.  * *** empty log message ***
  57.  *
  58.  * Revision 1.1  1995/03/09  08:48:06  allender
  59.  * Initial revision
  60.  *
  61.  *
  62.  * -------  PC version RCS information
  63.  * Revision 1.17  1994/11/18  22:50:25  john
  64.  * Changed shorts to ints in parameters.
  65.  * 
  66.  * Revision 1.16  1994/11/10  15:59:46  john
  67.  * Fixed bugs with canvas's being created with bogus bm_flags.
  68.  * 
  69.  * Revision 1.15  1994/10/26  23:55:53  john
  70.  * Took out roller; Took out inverse table.
  71.  * 
  72.  * Revision 1.14  1994/09/19  14:40:21  john
  73.  * Changed dpmi stuff.
  74.  * 
  75.  * Revision 1.13  1994/09/19  11:44:04  john
  76.  * Changed call to allocate selector to the dpmi module.
  77.  * 
  78.  * Revision 1.12  1994/06/09  13:14:57  john
  79.  * Made selectors zero our
  80.  * out, I meant.
  81.  * 
  82.  * Revision 1.11  1994/05/06  12:50:07  john
  83.  * Added supertransparency; neatend things up; took out warnings.
  84.  * 
  85.  * Revision 1.10  1994/04/08  16:59:39  john
  86.  * Add fading poly's; Made palette fade 32 instead of 16.
  87.  * 
  88.  * Revision 1.9  1994/03/16  17:21:09  john
  89.  * Added slow palette searching options.
  90.  * 
  91.  * Revision 1.8  1994/03/14  17:59:35  john
  92.  * Added function to check bitmap's transparency.
  93.  * 
  94.  * Revision 1.7  1994/03/14  17:16:21  john
  95.  * fixed bug with counting freq of pixels.
  96.  * 
  97.  * Revision 1.6  1994/03/14  16:55:47  john
  98.  * Changed grs_bitmap structure to include bm_flags.
  99.  * 
  100.  * Revision 1.5  1994/02/18  15:32:22  john
  101.  * *** empty log message ***
  102.  * 
  103.  * Revision 1.4  1993/10/15  16:22:49  john
  104.  * *** empty log message ***
  105.  * 
  106.  * Revision 1.3  1993/09/08  17:37:11  john
  107.  * Checking for errors with Yuan...
  108.  * 
  109.  * Revision 1.2  1993/09/08  14:46:27  john
  110.  * looking for possible bugs...
  111.  * 
  112.  * Revision 1.1  1993/09/08  11:43:05  john
  113.  * Initial revision
  114.  * 
  115.  *
  116.  */
  117.  
  118. #include <stdlib.h>
  119. #include <stdio.h>
  120.  
  121. #include "mem.h"
  122. #include "gr.h"
  123. #include "grdef.h"
  124.  
  125. grs_bitmap *gr_create_bitmap(int w, int h )
  126. {
  127.     grs_bitmap *new;
  128.  
  129.     new = (grs_bitmap *)mymalloc( sizeof(grs_bitmap) );
  130.     new->bm_x = 0;
  131.     new->bm_y = 0;
  132.     new->bm_w = w;
  133.     new->bm_h = h;
  134.     new->bm_flags = 0;
  135.     new->bm_type = 0;
  136.     new->bm_rowsize = w;
  137.     new->bm_selector = 0;
  138.  
  139.     new->bm_data = (unsigned char *)mymalloc_align( w*h, 8 );
  140.  
  141.     return new;
  142. }
  143.  
  144. grs_bitmap *gr_create_bitmap_raw(int w, int h, unsigned char * raw_data )
  145. {
  146.     grs_bitmap *new;
  147.  
  148.     new = (grs_bitmap *)mymalloc( sizeof(grs_bitmap) );
  149.     new->bm_x = 0;
  150.     new->bm_y = 0;
  151.     new->bm_w = w;
  152.     new->bm_h = h;
  153.     new->bm_flags = 0;
  154.     new->bm_type = 0;
  155.     new->bm_rowsize = w;
  156.     new->bm_data = raw_data;
  157.     new->bm_selector = 0;
  158.  
  159.     return new;
  160. }
  161.  
  162. void gr_init_bitmap( grs_bitmap *bm, int mode, int x, int y, int w, int h, int bytesperline, unsigned char * data )
  163. {
  164.     bm->bm_x = x;
  165.     bm->bm_y = y;
  166.     bm->bm_w = w;
  167.     bm->bm_h = h;
  168.     bm->bm_flags = 0;
  169.     bm->bm_type = mode;
  170.     bm->bm_rowsize = bytesperline;
  171.     bm->bm_data = data;
  172.     bm->bm_selector = 0;
  173. }
  174.  
  175.  
  176. grs_bitmap *gr_create_sub_bitmap(grs_bitmap *bm, int x, int y, int w, int h )
  177. {
  178.     grs_bitmap *new;
  179.  
  180.     new = (grs_bitmap *)mymalloc( sizeof(grs_bitmap) );
  181.     new->bm_x = x+bm->bm_x;
  182.     new->bm_y = y+bm->bm_y;
  183.     new->bm_w = w;
  184.     new->bm_h = h;
  185.     new->bm_flags = bm->bm_flags;
  186.     new->bm_type = bm->bm_type;
  187.     new->bm_rowsize = bm->bm_rowsize;
  188.     new->bm_data = bm->bm_data+(unsigned int)((y*bm->bm_rowsize)+x);
  189.     new->bm_selector = 0;
  190.  
  191.     return new;
  192. }
  193.  
  194.  
  195. void gr_free_bitmap(grs_bitmap *bm )
  196. {
  197.     if (bm->bm_data!=NULL)    
  198.         myfree(bm->bm_data);
  199.     bm->bm_data = NULL;
  200.     if (bm!=NULL)
  201.         myfree(bm);
  202. }
  203.  
  204. void gr_free_sub_bitmap(grs_bitmap *bm )
  205. {
  206.     if (bm!=NULL)
  207.         myfree(bm);
  208. }
  209.  
  210. void decode_data_asm(ubyte *data, int num_pixels, ubyte *colormap, int *count)
  211. {
  212.     int i;
  213.     
  214.     for (i = 0; i < num_pixels; i++) {
  215.         count[*data]++;
  216.         *data = colormap[*data];
  217.         data++;
  218.     }
  219. }
  220.  
  221. void build_colormap_good( ubyte * palette, ubyte * colormap, int * freq )
  222. {
  223.     int i, r, g, b;
  224.  
  225.     for (i=0; i<256; i++ )    {
  226.         r = *palette++;        
  227.         g = *palette++;        
  228.         b = *palette++;        
  229.          *colormap++ = gr_find_closest_color( r, g, b );
  230.         *freq++ = 0;
  231.     }
  232. }
  233.  
  234. void gr_remap_bitmap( grs_bitmap * bmp, ubyte * palette, int transparent_color, int super_transparent_color )
  235. {
  236.     ubyte colormap[256];
  237.     int freq[256], i, n;
  238.  
  239.     // This should be build_colormap_asm, but we're not using invert table, so...
  240.     build_colormap_good( palette, colormap, freq );
  241.  
  242.     if ( (super_transparent_color>=0) && (super_transparent_color<=255))
  243.         colormap[super_transparent_color] = 254;
  244.  
  245.     if ( (transparent_color>=0) && (transparent_color<=255))
  246.         colormap[transparent_color] = TRANSPARENCY_COLOR;
  247.  
  248.     n = bmp->bm_w * bmp->bm_h;
  249.     for (i = 0; i < n; i++)
  250.         bmp->bm_data[i] = colormap[bmp->bm_data[i]];
  251.     decode_data_asm(bmp->bm_data, bmp->bm_w * bmp->bm_h, colormap, freq );
  252.  
  253.     if ( (transparent_color>=0) && (transparent_color<=255) && (freq[transparent_color]>0) )
  254.         bmp->bm_flags |= BM_FLAG_TRANSPARENT;
  255.  
  256.     if ( (super_transparent_color>=0) && (super_transparent_color<=255) && (freq[super_transparent_color]>0) )
  257.         bmp->bm_flags |= BM_FLAG_SUPER_TRANSPARENT;
  258. }
  259.  
  260. void gr_remap_bitmap_good( grs_bitmap * bmp, ubyte * palette, int transparent_color, int super_transparent_color )
  261. {
  262.     ubyte colormap[256];
  263.     int freq[256];
  264.    
  265.     build_colormap_good( palette, colormap, freq );
  266.  
  267.     if ( (super_transparent_color>=0) && (super_transparent_color<=255))
  268.         colormap[super_transparent_color] = 254;
  269.  
  270.     if ( (transparent_color>=0) && (transparent_color<=255))
  271.         colormap[transparent_color] = TRANSPARENCY_COLOR;
  272.  
  273.     decode_data_asm(bmp->bm_data, bmp->bm_w * bmp->bm_h, colormap, freq );
  274.  
  275.     if ( (transparent_color>=0) && (transparent_color<=255) && (freq[transparent_color]>0) )
  276.         bmp->bm_flags |= BM_FLAG_TRANSPARENT;
  277.  
  278.     if ( (super_transparent_color>=0) && (super_transparent_color<=255) && (freq[super_transparent_color]>0) )
  279.         bmp->bm_flags |= BM_FLAG_SUPER_TRANSPARENT;
  280. }
  281.  
  282.  
  283. int gr_bitmap_assign_selector( grs_bitmap * bmp )
  284. {
  285.     bmp->bm_selector = 0;
  286.     return 0;
  287. }
  288.  
  289. void gr_bitmap_check_transparency( grs_bitmap * bmp )
  290. {
  291.     int x, y;
  292.     ubyte * data;
  293.  
  294.     data = bmp->bm_data;
  295.     
  296.     for (y=0; y<bmp->bm_h; y++ )    {
  297.         for (x=0; x<bmp->bm_w; x++ )    {
  298.             if (*data++ == TRANSPARENCY_COLOR )    {
  299.                 bmp->bm_flags = BM_FLAG_TRANSPARENT;
  300.                 return;
  301.             }
  302.         }
  303.         data += bmp->bm_rowsize - bmp->bm_w;
  304.     }
  305.  
  306.     bmp->bm_flags = 0;
  307.  
  308. }
  309.